package org.apache.solr.client;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QueryResults 
{
	private int _status;
	private int _qTime;
	private Map<String,String> _params = new HashMap<String, String>();
	private URL  _solrURL;

	private int _numFound;
	private int _start;
	private float _maxScore = 0;
	
	private List<ResultDoc>       _docs = new ArrayList<ResultDoc>( 15 );
	private Map<String,ResultDoc> _byID = new HashMap<String, ResultDoc>();
	private List<FieldFacet>   _facets = null;
	private DebugInfo          _debugInfo;
	private SolrQuery _query = null;

	public static class DebugInfo 
	{
		private String _rawQueryString;
		private String _queryString;
		private String _parsedQuery;
		private String _parsedQueryToString;
		
		public String getParsedQuery() {
			return _parsedQuery;
		}
		public void setParsedQuery(String parsedQuery) {
			_parsedQuery = parsedQuery;
		}
		public String getParsedQueryToString() {
			return _parsedQueryToString;
		}
		public void setParsedQueryToString(String parsedQueryToString) {
			_parsedQueryToString = parsedQueryToString;
		}
		public String getQueryString() {
			return _queryString;
		}
		public void setQueryString(String queryString) {
			_queryString = queryString;
		}
		public String getRawQueryString() {
			return _rawQueryString;
		}
		public void setRawQueryString(String rawQueryString) {
			_rawQueryString = rawQueryString;
		}
		
	}
	
	//------------------------------------------------------------
	//------------------------------------------------------------
	
	public void addDoc( ResultDoc doc )
	{
		_docs.add( doc );
		
		if( doc.getId() != null ) {
			_byID.put( doc.getId(), doc );
		}
	}

	public void addFieldFacet( FieldFacet facet )
	{
		if( _facets == null ) {
			_facets = new ArrayList<FieldFacet>(5);
		}
		_facets.add( facet );
	}
	
	public ResultDoc getFirstResult()
	{
		if( _docs != null && _docs.size() > 0 ) {
			return _docs.get( 0 );
		}
		return null;
	}

	//------------------------------------------------------------
	//------------------------------------------------------------

	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append( "Result[status:"+_status );
		if( _docs != null ) {
			builder.append( _docs );
		}
		builder.append( "]" );
		return builder.toString();
	}

	//------------------------------------------------------------
	//------------------------------------------------------------

	public ResultDoc getDoc( String id )
	{
		return _byID.get( id );
	}

	//------------------------------------------------------------
	//------------------------------------------------------------

	public List<ResultDoc> getDocs() {
		return _docs;
	}

	public float getMaxScore() {
		return _maxScore;
	}

	public void setMaxScore(float maxScore) {
		_maxScore = maxScore;
	}

	public int getNumFound() {
		return _numFound;
	}

	public void setNumFound(int numFound) {
		_numFound = numFound;
	}

	public int getQTime() {
		return _qTime;
	}

	public void setQTime(int time) {
		_qTime = time;
	}

	public int getStart() {
		return _start;
	}

	public void setStart(int start) {
		_start = start;
	}

	public int getStatus() {
		return _status;
	}

	public void setStatus(int status) {
		_status = status;
	}

	public DebugInfo getDebugInfo() {
		return _debugInfo;
	}

	public void setDebugInfo(DebugInfo debugInfo) {
		_debugInfo = debugInfo;
	}

	public Map<String, String> getParams() {
		return _params;
	}

	public void setParams(Map<String, String> params) {
		_params = params;
	}

	public URL getSolrURL() {
		return _solrURL;
	}

	public void setSolrURL(URL solrURL) {
		_solrURL = solrURL;
	}

	public List<FieldFacet> getFacets() {
		return _facets;
	}

	public List<FieldFacet> getLimitingFacets() {
		if( _facets != null ) {
			List<FieldFacet> lim = new ArrayList<FieldFacet>( _facets.size() );
			for( FieldFacet f : _facets ) {
				FieldFacet limiting = f.getLimitingFields( _numFound );
				if( limiting.getValueCount() > 1 ) {
					lim.add( limiting );
				}
			}
			return lim;
		}
		return null;
	}

	public void removeFacets() {
		_facets = null;
	}

	public SolrQuery getQuery() {
		return _query;
	}

	public void setQuery(SolrQuery query) {
		_query = query;
	}
}
